import 'dart:async';
import 'dart:math';

import 'package:flutter/material.dart';
import 'package:xvideos/hj_page/splash/splash_banner_widget.dart';
import 'package:xvideos/hj_utils/screen.dart';

import '../../hj_model/splash/ads_model.dart';
import '../../routers/jump_router.dart';
import '../../tools_base/widget/net_image_widget.dart';

class SplashAdView extends StatefulWidget {
  final List<AdsInfoModel>? adData;
  final Function? callback;

  const SplashAdView({
    super.key,
    this.adData,
    this.callback,
  });

  @override
  State<StatefulWidget> createState() {
    return _SplashAdViewState();
  }
}

class _SplashAdViewState extends State<SplashAdView> {
  int count = 5;
  Timer? timer;

  @override
  void initState() {
    super.initState();

    timer = Timer.periodic(const Duration(seconds: 1), (timer) {
      if (count <= 0) {
        timer.cancel();
        return;
      }
      count--;
      setState(() {});
    });
  }

  @override
  void dispose() {
    timer?.cancel();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Stack(
      fit: StackFit.expand,
      children: <Widget>[
        if (widget.adData?.length == 1)
          GestureDetector(
            onTap: () async {
              pushToPageByLink(widget.adData?.first.href ?? "", id: widget.adData?.first.id);
            },
            child: Container(
              alignment: Alignment.center,
              child: NetworkImageLoader(
                imageUrl: widget.adData?.first.cover ?? "",
                width: screen.screenWidth,
                height: screen.screenHeight,
                fit: BoxFit.cover,
                placeHolderWidget: Image.asset(
                  "assets/images/launch/ic_splash_bg.webp",
                  fit: BoxFit.fill,
                  width: screen.screenWidth,
                  height: screen.screenHeight,
                ),
              ),
            ),
          )
        else if ((widget.adData?.length ?? 0) > 1)
          Container(
            color: Color(0xff151515),
            child: SplashBannerWidget(adData: widget.adData ?? []),
          )
        else
          SizedBox(
            width: screen.screenWidth,
            height: screen.screenHeight,
            child: Image.asset(
              'assets/images/launch/ic_splash_bg.webp',
              fit: BoxFit.fill,
            ),
          ),
        SafeArea(
          child: Stack(
            children: <Widget>[
              Positioned(
                top: 28,
                right: 17,
                child: GestureDetector(
                  child: Container(
                      height: 36,
                      width: 77,
                      alignment: Alignment.center,
                      decoration: BoxDecoration(
                        color: Colors.black.withOpacity(0.5),
                        borderRadius: BorderRadius.circular(18),
                      ),
                      child: (count > 0)
                          ? Text(
                              min(5, count).toString(),
                              style: const TextStyle(
                                color: Colors.white,
                                fontSize: 18,
                                fontWeight: FontWeight.w600,
                              ),
                            )
                          : Text(
                              "关闭",
                              style: const TextStyle(
                                color: Colors.white,
                                fontSize: 16,
                              ),
                            )),
                  onTap: () {
                    if (count <= 0) {
                      widget.callback?.call();
                    }
                  },
                ),
              ),
            ],
          ),
        ),
      ],
    );
  }
}
